package org.finmanager.budget;

import org.finmanager.core.CategoryModel;
import org.finmanager.core.FinManager;

import javax.swing.*;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * Created by Kelvin on 4/4/2014.
 */

//A class to manage the planning function by getting different models and budgetManagerUI
public class BudgetManagerController implements ActionListener {
	public static final String ADD_BUDGET = "ADD_BUDGET";
	public static final String DELETE_BUDGET = "DELETE_BUDGET";

	public static final String OPEN_BUDGET_INPUT_UI = "OPEN_BUDGET_INPUT_UI";

	private final BudgetModel budgetModel;
	private final CategoryModel categoryModel;
	private final BudgetManagerUI budgetManagerUI;

	private BudgetInputUI budgetInputUI;

	public BudgetManagerController(FinManager finManager) {
		this.budgetModel = finManager.getBudgetModel();
		this.categoryModel = finManager.getCategoryModel();
		budgetManagerUI = new BudgetManagerUI(this);

		budgetManagerUI.addButtonListener(getActionListener());
	}

	public BudgetModel getBudgetModel() {
		return budgetModel;
	}

	public CategoryModel getCategoryModel() {
		return categoryModel;
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		String actionCommand = e.getActionCommand();
		if (actionCommand.equals(ADD_BUDGET)) {
			budgetModel.addBudget(budgetInputUI.getBudgetFromInput());
		} else if (actionCommand.equals(DELETE_BUDGET)) {
			budgetModel.deleteBudget(budgetManagerUI.getSelectedRowInModel());
		} else if (actionCommand.equals(OPEN_BUDGET_INPUT_UI)) {
			budgetInputUI = new BudgetInputUI(this);
		}
	}

	public JComponent getUIMainComponent() {
		return budgetManagerUI.getMainComponent();
	}

	public ActionListener getActionListener() {
		return this;
	}
}
